////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                //
//                    Sequence list's find, insert, delete, etc...functions' implemention code               //
//                                                                                                                                //
//                                                                                                                                //
//                        Author:    Jack Hui (huiguobin)                                                               //
//                                                                                                                                //
//                                                                                                                                //
////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////functions' implemention file////////////////////////////////

#include "sequence_list.h"

/*Print Sequence List*/
void print_seq(SeqList *L)
{
	int i;

	printf("The seqlist now is : \n");
	for(i = 0; i != L->size; ++i)
	{
		printf("%d ", L->data[i]);
	}
	printf("\n\n");
}

/*Sequence List's finding*/
int find_seq(SeqList L, DataType key_data, int *pos)
{
	int i;

	if(L.size <= 0)
	{
		return 0;
	}

	for(i = 0; i != L.size; ++i)
	{
		if(L.data[i] == key_data)
		{
			*pos = i;
			return 1;
		}
	}
	
	return 0;
}


/*Sequence List's insert*/
int insert_seq(SeqList *L, int pos, DataType insert_data)
{
	int i;

	if(pos<0 || pos > L->size-1)
	{
		return 0;
	}

	/*Start from the last elment.*/
	for(i = L->size-1; i >= pos; --i)
	{
		L->data[i+1] = L->data[i];
	}

	L->data[pos] = insert_data;
	L->size++;
	return 1;
}


/*Sequence List's delete.*/
int delete_seq(SeqList *L, int pos, DataType *del_data)
{
	int i;

	if(pos<0 || pos > L->size-1)
	{
		return 0;
	}

	*del_data = L->data[pos];
	
	/*Start counting from the inserting position's first element */
	for(i = pos+1; i != L->size; ++i)
	{
		L->data[i-1] = L->data[i];
	}
	L->size--;
	return 1;
}

/*Sequence List's reverse.*/
int reverse_seq(SeqList *L)
{
	int low = 0;
	int high = L->size - 1;
	DataType temp = 0;

	if(L->size<0 || L->size>max_Size)
	{
		return 0;
	}



	while(low < high)
	{
		temp = L->data[high];
		L->data[high] = L->data[low];
		L->data[low] = temp;
		low++;
		high--;
	}
	return 1;
	
}
